package com.xusheng.cli.controller.back;

import cn.hutool.core.util.StrUtil;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.frontUserModule.VipConfig;
import com.xusheng.cli.req.VipConfigQueryReq;
import com.xusheng.cli.service.VipConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 会员管理相关接口
 *
 * @author lyh
 * @since 2023-04-24 15:35
 */
@RestController
@RequestMapping("/api/v1/vip")
public class VipConfigController {

    @Resource
    private VipConfigService vipService;

    /**
     * 通过条件查询vip列表
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("")
    public Result<MyPage<VipConfig>> list(VipConfigQueryReq req) {
        return Result.success(vipService.lambdaQuery()
                .like(StrUtil.isNotBlank(req.getName()), VipConfig::getName, req.getName())
                .orderByDesc(VipConfig::getCreateTime)
                .page(req.getPage()));
    }

    /**
     * 保存vip配置
     *
     * @param config vip信息
     * @return 返回结果
     */
    @PostMapping("")
    public Result<Object> save(@Validated(Save.class) @RequestBody VipConfig config) {
        return Result.isSuccess(vipService.save(config));
    }

    /**
     * 更新vip配置
     *
     * @param config vip信息
     * @return 返回结果
     */
    @PutMapping("")
    public Result<Object> update(@Validated(Update.class) @RequestBody VipConfig config) {
        return Result.isSuccess(vipService.updateById(config));
    }

    /**
     * 通过id获取vip配置
     *
     * @param id 编号
     * @return 返回结果
     */
    @GetMapping("/{id}")
    public Result<VipConfig> info(@PathVariable long id) {
        return Result.success(vipService.getById(id));
    }

    /**
     * 通过id删除vip配置
     *
     * @param id 编号
     * @return 返回结果
     */
    @DeleteMapping("/{id}")
    public Result<Object> delete(@PathVariable long id) {
        return Result.isSuccess(vipService.removeById(id));
    }
}
